package com.wa.util;

import com.wa.exception.DingTalkException;
import com.wa.pojo.GenericResp;
import com.wa.pojo.GraphqlPojo;
import com.wa.pojo.Page;
import com.wa.pojo.PagePojo;

/**
 * @Description 结果返回工具类
 * @Author Dawn
 * @Date 2021/6/10 16:33
 */
public class ResultPojoUtil {

    /**
     * @Description 构建PagePojo
     * @Author Dawn
     * @Date 2021/6/10 16:35
     */
    public static <T> PagePojo<T> buildPagePojo(GenericResp<Page<T>> genericResp) {

        if (genericResp.getCode().intValue() != 0) {
            throw new DingTalkException(genericResp.getMessage(), genericResp.getCode());
        }

        PagePojo<T> pagePojo = new PagePojo();

        pagePojo.setCode(genericResp.getCode());
        pagePojo.setMessage(genericResp.getMessage());

        if (genericResp.getData() == null) {
            pagePojo.setPageNo(genericResp.getData().getPageNo());
            pagePojo.setPageSize(genericResp.getData().getPageSize());
            pagePojo.setTotalPage(genericResp.getData().getTotalPageCount());
            pagePojo.setTotalCount(((Long) genericResp.getData().getTotalCount()).intValue());
            pagePojo.setData(genericResp.getData().getResult());
        }

        return pagePojo;
    }

    /**
     * @Description 构建GraphqlPojo
     * @Author Dawn
     * @Date 2021/6/15 11:58
     */
    public static <T> GraphqlPojo<T> buildGraphqlPojo(GenericResp<T> genericResp) {

        if (genericResp.getCode().intValue() != 0) {
            throw new DingTalkException(genericResp.getMessage(), genericResp.getCode());
        }

        GraphqlPojo graphqlPojo = new GraphqlPojo();
        graphqlPojo.setData(genericResp.getData());
        graphqlPojo.setCode(genericResp.getCode());
        graphqlPojo.setMessage(genericResp.getMessage());
        return graphqlPojo;
    }

}
